package com.medical.lsq.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.lsq.dao.SysRoleMapper;
import com.medical.lsq.entity.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class RoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    /**
     * 添加区域
     * @param role
     */
    public void add(SysRole role) {
        role.getCreateTime(new Timestamp(System.currentTimeMillis()));
        role.getUpdateTime(new Timestamp(System.currentTimeMillis()));
        roleMapper.insert(role);
    }

    /**
     * 删除区域
     * @param id
     */
    public void delete(Integer id) {
        if(id !=null) {
            roleMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 修改区域
     * @param role
     */
    public void edit(SysRole role)  {
        role.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 根据ID主键查找区域信息
     * @param id
     * @return
     */
    public SysRole getRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }
    /**
     * 分页查找
     * @param roleName
     * @param currentPage
     * @param limit
     * @return
     */
    public PageInfo<SysRole> getRoleList(String roleName, Integer currentPage, Integer limit) {
        //利用PageHelper分页查询 注意：这个一定要放在查询语句的第一行，否者无法进行分页，因为他对金随后的第一个sql语句
        PageHelper.startPage(currentPage,limit);
        List<SysRole> list = roleMapper.getRoleList(roleName);
        PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(list);
        return pageInfo;
    }
    public List<SysRole> getRoleTree(String roleName) {
        return roleMapper.getRoleList(roleName);
    }


}
